package com.howie.gulimall.ware.service.impl;

import com.howie.common.utils.R;
import com.howie.gulimall.ware.feign.ProductFeignService;
import com.howie.gulimall.ware.vo.SkuHasStockVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.howie.common.utils.PageUtils;
import com.howie.common.utils.Query;

import com.howie.gulimall.ware.dao.WareSkuDao;
import com.howie.gulimall.ware.entity.WareSkuEntity;
import com.howie.gulimall.ware.service.WareSkuService;

import javax.annotation.Resource;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {
    @Resource
    WareSkuDao wareSkuDao;
    @Autowired
    ProductFeignService productFeignService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
        String skuId = (String) params.get("skuId");
        if(StringUtils.isNotEmpty(skuId)){
            wrapper.eq("sku_id",skuId);
        }
        String wareId = (String) params.get("wareId");
        if(StringUtils.isNotEmpty(wareId)){
            wrapper.eq("ware_id",wareId);
        }
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        // 判断有没有skuId和ware_id对应的库存记录
        List<WareSkuEntity> wareSkuEntities = this.baseMapper.selectList(new QueryWrapper<WareSkuEntity>()
                .eq("sku_id",skuId).eq("ware_id",wareId));
        if(wareSkuEntities == null || wareSkuEntities.size() == 0){
            // 新建
            WareSkuEntity wareSkuEntity =new WareSkuEntity();
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStock(skuNum);
            wareSkuEntity.setStockLocked(0);
            // 设置sku_name 需要远程调用商品服务接口(如果失败整个事务不需要回滚)
            try{
                R r = productFeignService.info(skuId);
                if(r.getCode() == 0){
                    // 请求成功
                    Map<String,Object> data = (Map<String, Object>) r.get("skuInfo");
                    wareSkuEntity.setSkuName(data.get("skuName").toString());
                }
            } catch (Exception e){
                e.printStackTrace();
            }
            this.baseMapper.insert(wareSkuEntity);
        }else{
            // 更新
            wareSkuDao.updateStock(skuId,wareId,skuNum);
        }
    }

    @Override
    public List<SkuHasStockVo> getSkuHasStock(List<Long> skuIds) {
        List<SkuHasStockVo> skuHasStockVoList = skuIds.stream().map(skuId -> {
            SkuHasStockVo skuHasStockVo = new SkuHasStockVo();
            skuHasStockVo.setSkuId(skuId);
            // 获取sku的库存时，必须检索所有仓库，并且要减去下了单的数量(以锁定的商品)
            // SELECT SUM(stock - stock_locked) FROM `wms_ware_sku` WHERE sku_id = 14;
            Long count = this.baseMapper.getSkuStock(skuId);
            skuHasStockVo.setHasStock(count != null && count > 0);
            return skuHasStockVo;
        }).collect(Collectors.toList());
        return skuHasStockVoList;
    }

}